package cn.new7as.cas5s.ui.record;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.List;

import cn.new7as.cas5s.R;

public class SendsFileMsgadapter extends BaseAdapter {

    private List<String> list;
    private Activity context;
    private int type;


    public SendsFileMsgadapter(List<String> list, Activity context) {
        super();
        this.context = context;
        this.list = list;
    }

    public SendsFileMsgadapter(List<String> list, Activity context, int type) {
        super();
        this.context = context;
        this.list = list;
        this.type = type;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }


    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        ViewHolder viewHolder = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.layout_gride_file, parent, false);
            viewHolder = new ViewHolder();
            viewHolder.upload = (TextView) convertView.findViewById(R.id.upload);
            viewHolder.clear = (ImageView) convertView.findViewById(R.id.clear_text);
            convertView.setTag(viewHolder);

        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        if (type != 0) viewHolder.clear.setVisibility(View.GONE);
        viewHolder.clear.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (context instanceof SendOrderMsgActivity)
                    ((SendOrderMsgActivity) context).deleFile(list.get(position), position);

                if (context instanceof SendMsgActivity)
                    ((SendMsgActivity) context).deleFile(list.get(position), position);
            }
        });
        if (type == 1) {
            if (list.get(position).contains("/")) {
                String[] split = list.get(position).split("/");
                String s = split[split.length - 1];
                viewHolder.upload.setText(s);
            } else viewHolder.upload.setText(list.get(position));
        } else
            viewHolder.upload.setText(list.get(position));

        return convertView;
    }

    static class ViewHolder {
        TextView upload;
        ImageView clear;

    }
}
